window.onload = function () {

    var indexbtn = document.querySelector(`.logo-c`)
    indexbtn.onclick = function () {
    location.href = "index.html"
}
    
       


    var loginbtn = document.querySelector(`.login-button-box`)
    console.log(loginbtn)
    loginbtn.onclick = function () {
        var username = document.querySelector(`[name="ui-form"]`).value;
        var password = document.querySelector(`[name="password"]`).value;
        var check = document.querySelector(`.check`)
        console.log(111)

        var reg = /^1[3-9]\d{9}$/
        if (!reg.test(username)) {
            alert(`用户名不规范`)
            return false
        }
        if (password === ``) {
            alert(`密码不规范`)
            return false

        }

        if (!check.checked) {
            alert(`请勾选协议`)
            return false
        }

        var xhr = new XMLHttpRequest;
        xhr.open(`post`, `/login`)
        xhr.setRequestHeader(`content-type`, `application/x-www-form-urlencoded`)
        xhr.send(`username=${username}&password=${password}`)
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                if (xhr.status >= 200 && xhr.status < 300) {
                    var res = xhr.responseText;
                    console.log(res);
                    res = JSON.parse(res)
                    if (res.code == 1) {
                        setCookie(`username`, username, 7200)
                        alert(res.msg)
                        location.href = "index.html"
                    } else {
                        alert(res.msg)
                    }
                }
            }
        }
    }

}